import jieba
txt=open("test.txt","r",encoding='utf-8').read()
words=jieba.lcut(txt)
counts={}
for word in  words:
    if len(word)==1:
        continue
    elif word=="大圣" or word=="老孙" or word=="悟空" or word=="行者":
        rword="孙悟空"
    elif word=="师父" or word=="三藏":
        rword="唐僧"
    elif word=="呆子":
        rword=="八戒"
    else:
        rword=word
    counts[rword]=counts.get(rword,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
fo=open("output.txt","a+",encoding='utf-8')
for i in range(5):
    word,count=items[i]
    fo.writelines(word+":"+str(count)+"\n")
    print("{0:<10}{1:>5}".format(word,count))
